package com.hmall.gateway.filters;

import lombok.Data;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.OrderedGatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Classname PrintAnyGatewayFilterFactory
 * @Date 2024/11/30 16:38
 * @Created by 鲍宪立
 */
@Component
public class PrintAnyGatewayFilterFactory extends AbstractGatewayFilterFactory<PrintAnyGatewayFilterFactory.Config> {
    @Override
    public GatewayFilter apply(Config config) {
        return new OrderedGatewayFilter((exchange, chain) -> {
            System.out.println("print any running");
            System.out.println(config.toString());
            return chain.filter(exchange);
        }, 1);
    }

    //带参数过滤器定义方式，需要实现shortcutFieldOrder()方法，定义返回值的顺序
    @Override
    public List<String> shortcutFieldOrder() {
        return List.of("a", "b", "c");
    }
    //构造方法，需要传入Config类，否则无法加载Config类中的属性
    public PrintAnyGatewayFilterFactory() {
        super(Config.class);
    }
    @Data
    public static class Config {
        private String a;
        private String b;
        private String c;
    }
}